package cn.lili.modules.serviceimpl;

import cn.lili.common.exception.ServiceException;
import cn.lili.common.utils.StringUtils;
import cn.lili.modules.entity.AppUser;
import cn.lili.modules.exception.MyAssert;
import cn.lili.modules.mapper.AppUserMapper;
import cn.lili.modules.service.AppUserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

/**
 * @author:xiangqian
 * @date: 2024/3/8 15:17
 */
@Service
public class AppUserServiceImpl extends ServiceImpl<AppUserMapper, AppUser> implements AppUserService {

    /**
     * 根据token查询用户信息
     *
     * @param token
     * @return 当前token关联的用户信息
     */
    @Override
    public AppUser getAppUserByToken(String token) {
        //查询token是否存在
        MyAssert.notNull(token, "TOKEN不能为空");

        //根据token查询用户
        LambdaQueryWrapper<AppUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AppUser::getToken, token);
        queryWrapper.last("limit 1");
        AppUser appUser = this.getBaseMapper().selectOne(queryWrapper);

        //校验appUser是否存在
        MyAssert.notNull(appUser, "用户不存在");
        return appUser;
    }

    /**
     * 根据token查询用户信息
     *
     * @param token
     * @return 当前token关联的用户信息
     */
    @Override
    public AppUser getAppUserByTokenNot(String token) {
        //根据token查询用户
        LambdaQueryWrapper<AppUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AppUser::getToken, token);
        queryWrapper.last("limit 1");
        return this.getBaseMapper().selectOne(queryWrapper);
    }

    /**
     * 根据openid查询用户信息
     *
     * @param userId
     * @return
     */
    @Override
    public AppUser getAppUserBy(String userId) {
        LambdaQueryWrapper<AppUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AppUser::getOpenid, userId);
        queryWrapper.last("limit 1");
        return this.getBaseMapper().selectOne(queryWrapper);
    }
}
